package dao;

import bean.Borrow;
import db.MyHibernateSessionFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by WeaponLin on 2015/11/19.
 */
public class BorrowDAO {

    public List<Borrow> queryBorrowByReaderID(String readerid) {
        return queryBorrowBySql("readerid='" + readerid + "'");
    }

    public List<Borrow> queryHistoryBorrow(String readerid) {
        return queryBorrowBySql("readerid='" + readerid + "' and dateActual is not null");
    }

    public List<Borrow> queryNoReturnBorrow(String readerid) {
        return queryBorrowBySql("readerid='" + readerid + "' and dateActual is null");
    }

    public List<Borrow> queryBorrowBySql(String sql) {
        Transaction transaction = null;
        List<Borrow> list = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();// 开启事务
            String sqlStr = "from Borrow where " + sql;
            Query query = session.createQuery(sqlStr);
            list = query.list();
            transaction.commit();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return list;
        }
    }

    public boolean addBorrow(Borrow borrow) {
        Transaction transaction = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();
            session.save(borrow);
            transaction.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return false;
        }
    }

    public boolean updateBorrow(Borrow borrow) {
        Transaction transaction = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();
            session.update(borrow);
            transaction.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return false;
        }
    }

    public boolean deleteBorrow(String readerid,String bookid) {
        Transaction transaction = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();
            Query query = session.createQuery("from Borrow where reader.readerid='" + readerid + "' and book.bookid='"+bookid+"'");
            List<Borrow> list = query.list();
            for (Borrow borrow : list) {
                session.delete(borrow);
            }
            transaction.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return false;
        }
    }
}
